package cn.lcvc.xnc.authority.web.controller.backstage.authority;


import cn.lcvc.xnc.authority.model.SysRole;
import cn.lcvc.xnc.authority.model.param.RoleEnabledParam;
import cn.lcvc.xnc.authority.model.param.RolePermissionParam;
import cn.lcvc.xnc.authority.model.query.SysRoleQuery;
import cn.lcvc.xnc.authority.service.SysRoleService;
import cn.lcvc.xnc.authority.web.controller.backstage.BaseBackstageController;
import cn.lcvc.xnc.application.common.model.result.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@Api(tags = "后台角色管理模块")
@RestController
@RequestMapping(value = "/api/backstage/authority/role")
public class SysRoleController extends BaseBackstageController<SysRole, SysRoleQuery,Integer> {

    @Autowired
    private SysRoleService baseService;

    @PreAuthorize("hasAuthority('sys:role:query')")
    @GetMapping("/all")
    @Override
    public ResponseData query(SysRoleQuery sysRoleQuery){
        return super.query(sysRoleQuery);
    }


    @ApiOperation(value = "分页查询角色列表")
    @PreAuthorize("hasAuthority('sys:role:query')")
    @Override
    public ResponseData query(Integer page, Integer pageSize, SysRoleQuery sysRoleQuery){
        return super.query(page,pageSize,sysRoleQuery);
    }


    @ApiOperation(value = "批量删除指定的多个角色")
    @PreAuthorize("hasAuthority('sys:role:del')")
    @Override
    public ResponseData deletes(@NotEmpty(message="请选择要移除的记录")Integer[] ids){
        return super.deletes(ids);
    }

    @ApiOperation(value = "读取指定角色信息")
    @PreAuthorize("hasAuthority('sys:role:query')")
    @Override
    public ResponseData get(@PathVariable Integer id) {
        return super.get(id);
    }

    @ApiOperation(value = "编辑角色")
    @PreAuthorize("hasAuthority('sys:role:edit')")
    @Override
    public ResponseData edit(@Valid @RequestBody SysRole sysRole){
        return super.edit(sysRole);
    }

    @ApiOperation(value = "添加角色")
    @PreAuthorize("hasAuthority('sys:role:add')")
    @Override
    protected ResponseData add(@Valid @RequestBody SysRole sysRole) {
       return super.add(sysRole);
    }


    //=========================扩展部分=============================================//

    @ApiOperation(value = "批量更改指定的多个角色的可用属性")
    @PreAuthorize("hasAuthority('sys:role:edit')")
    @PatchMapping("/edit/enabled")
    public ResponseData updateEnabled(@Valid @RequestBody RoleEnabledParam roleEnabledParam){
        baseService.updateEnabledByIdIn(roleEnabledParam.getIds(),roleEnabledParam.getEnabled());
        return ResponseData.success(null);
    }

    //======================================权限操作=====================================================//
    @ApiOperation(value = "获取角色已有的菜单id")
    @PreAuthorize("hasAuthority('sys:role:permission:query')")
    @GetMapping("/permission/{roleId}")
    protected ResponseData permissionManage(@PathVariable @NotNull(message="roleId参数不能未空") Integer roleId) {
        return ResponseData.success(baseService.getPermissionIdsByRoleId(roleId));
    }

    @ApiOperation(value = "角色的权限分配")
    @PreAuthorize("hasAuthority('sys:role:permission:edit')")
    @PutMapping("/permission")
    protected ResponseData permissionManage(@Valid @RequestBody RolePermissionParam rolePermissionParam) {
        baseService.updateRolePermission(rolePermissionParam.getRoleId(),rolePermissionParam.getPermissionIds());
        return ResponseData.success(null);
    }
}
